import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2025-01-14
 * Time: 21:13
 */
public class IsAnagram {
    public boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        Map<Character, Integer> map = new HashMap<>();
        for (char c :
                s.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0)+1);
        }
        for (char c :
                t.toCharArray()) {
            if (!map.containsKey(c)) {
                return false;
            }
            int n = map.get(c);
            if (n == 0) {
                return false;
            } else {
                map.put(c, n-1);
            }
        }
        return true;
    }

    public static void main(String[] args) {
        IsAnagram isAnagram = new IsAnagram();
        isAnagram.isAnagram("anagram", "nagaram");
    }
}
